package com.huidu.bitrade.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huidu.bitrade.constant.SysConstant;
import com.huidu.bitrade.dto.AcMemberQuery;
import com.huidu.bitrade.dto.AcMemberRecordDTO;
import com.huidu.bitrade.dto.AcReferrerRewardsDTO;
import com.huidu.bitrade.dto.RankNumberDTO;
import com.huidu.bitrade.entity.AcNodeHdz;
import com.huidu.bitrade.entity.transform.AuthMember;
import com.huidu.bitrade.exception.ServiceException;
import com.huidu.bitrade.pagination.PageResult;
import com.huidu.bitrade.result.page.PageCondition;
import com.huidu.bitrade.service.AcMemberService;
import com.huidu.bitrade.service.AcNodeHdzService;
import com.huidu.bitrade.service.LocaleMessageSourceService;
import com.huidu.bitrade.util.MessageResult;
import com.huidu.bitrade.util.RedisUtilUse;
import com.huidu.bitrade.vo.AcHdcAwardVO;
import com.huidu.bitrade.vo.AcMemberRecordVo;
import com.huidu.bitrade.vo.AcMemberStarRecordVo;
import com.huidu.bitrade.vo.AcReferrerRewardsVO;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

import static com.huidu.bitrade.constant.SysConstant.SESSION_MEMBER;

@Api(tags = "新增奖励模块/创世节点购买")
@RestController
@RequiredArgsConstructor
@Slf4j
public class NodePurchaseController {

    private final AcMemberService acMemberService;
    private final LocaleMessageSourceService msService;
    private final AcNodeHdzService acNodeHdzService;
    private final RedisUtilUse redisUtilUse;

    @Value("${node.buy}")
    private Integer nodeBuy;

    @ApiOperation(value = "购买")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "count",value = "购买份数",paramType = "query"),
            @ApiImplicitParam(name = "payType",value = "支付方式 1、币本位hdz 70%，2、金本位cnhd 30%",paramType = "query")
    })
    @PostMapping("purchase")
    public MessageResult purchase(@SessionAttribute(SESSION_MEMBER)AuthMember authMember,
                                  @RequestParam("count") Integer count,
                                  @RequestParam("payType") Integer payType) throws ServiceException {
        //开启
        if(nodeBuy.equals(1)){
            String key = SysConstant.MEMBER_BUY_NODE + authMember.getId()+count;
            boolean flag = redisUtilUse.setIfAbsent(key, authMember.getMobilePhone(), 5);
            if(flag){
                // 缓存验证码
                acMemberService.purchase(authMember,count,payType);
                return MessageResult.success();
            }else{
                throw new ServiceException("正在购买，请勿重复操作");
            }
        }
        throw new IllegalArgumentException(msService.getMessage("NOT_OPEN_YET"));
    }

    @ApiOperation(value = "查询可购买节点hdz")
    @GetMapping("getAcBalance")
    public MessageResult<BigDecimal> getAcBalance(@SessionAttribute(SESSION_MEMBER)AuthMember authMember){
        AcNodeHdz acNodeHdz = acNodeHdzService.getOne(Wrappers.<AcNodeHdz>lambdaQuery().eq(AcNodeHdz::getMemberId,authMember.getId()));
        if(Objects.isNull(acNodeHdz)){
            return MessageResult.success("SUCCESS",BigDecimal.ZERO);
        }else{
            return MessageResult.success("SUCCESS",acNodeHdz.getHdz());
        }
    }

    @ApiOperation(value = "获得当前已购买的个数")
    @GetMapping("getCurrCount")
    public MessageResult getCurrCount(@SessionAttribute(SESSION_MEMBER)AuthMember authMember){
        Integer currCount = acMemberService.getCurrCount(authMember);
        return MessageResult.success("SUCCESS",currCount);
    }

    /**
     * 在用户购买时，发放推荐人奖励，不是单独取处理
     */
//    @ApiOperation(value = "给推荐人发放奖励")
//    @GetMapping("sendAwardByParent")
//    public MessageResult sendAwardByParent(){
//        purchaseService.distribute();
//        return MessageResult.success();
//    }

    /**
     * 测试接口，发放hdc奖励
     * todo 需要改成定时任务
     */
    @ApiOperation(value = "发放hdc奖励",notes = "该接口仅作测试")
    @GetMapping("hdcAward")
    public MessageResult hdcAward(){
        acMemberService.hdcAward();
        return MessageResult.success();
    }

    @ApiOperation("获取hdz价格")
    @GetMapping("getHdzMoney")
    public MessageResult getHdcMoney() {
        BigDecimal currentHdz = acMemberService.getCurrentHdz();
        return MessageResult.success("SUCCESS",currentHdz);
    }

   @ApiOperation("获取每个等级下的用户数量")
    @GetMapping("getRankNumber")
    public MessageResult<List<RankNumberDTO>> getRankNumber(){
        return MessageResult.success("SUCCESS",acMemberService.getRankNumber());
    }


    @ApiOperation("查询创世节点升星奖励")
    @GetMapping("getMemberStarRecord")
    public PageResult<AcMemberStarRecordVo> getMemberStarRecord(AcMemberQuery acMemberQuery){
        return new PageResult<>(acMemberService.getMemberStarRecordVos(PageCondition.page(acMemberQuery),acMemberQuery));
    }

    @ApiOperation("HDC奖励")
    @GetMapping("getHdcRecord")
    public PageResult<AcHdcAwardVO> getHdcRecord(AcMemberQuery acMemberQuery){
        return new PageResult<>(acMemberService.getHdcRecord(PageCondition.page(acMemberQuery), acMemberQuery));
    }

    @ApiOperation("创世节点购买记录")
    @GetMapping("getMemberRecord")
    public PageResult<AcMemberRecordVo> getMemberRecord(AcMemberRecordDTO memberRecordDTO){
        return new PageResult<>(acMemberService.getMemberRecord(PageCondition.page(memberRecordDTO), memberRecordDTO));
    }

    @ApiOperation("创世节点信息查询")
    @GetMapping("getReferre")
    public PageResult<AcReferrerRewardsVO> getReferre(AcReferrerRewardsDTO referrerRewardsDTO){
        return new PageResult<>(acMemberService.getRewards(PageCondition.page(referrerRewardsDTO), referrerRewardsDTO));
    }

}
